package com.css.fxfzypg.manage.building.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzypg.manage.building.repository.entity.BuildingEntity;
import com.css.fxfzypg.manage.sampling.service.SamplingService;
import com.css.fxfzypg.modules.surveytaskmanage.service.FieldSurveyTaskService;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class BuildingExcelVerifyHandler implements IExcelVerifyHandler<BuildingEntity> {

    @Autowired
    FieldSurveyTaskService fieldSurveyTaskService;
    @Autowired
    SamplingService samplingService;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(BuildingEntity buildingEntity) {

        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");
        //空行跳过检验
        if(PlatformObjectUtils.isEmpty(buildingEntity.getTaskCode()) && PlatformObjectUtils.isEmpty(buildingEntity.getProvince()) && PlatformObjectUtils.isEmpty(buildingEntity.getCity()) && PlatformObjectUtils.isEmpty(buildingEntity.getCounty()) && PlatformObjectUtils.isEmpty(buildingEntity.getCountry()) && PlatformObjectUtils.isEmpty(buildingEntity.getLongitude()) && PlatformObjectUtils.isEmpty(buildingEntity.getLatitude())
                && PlatformObjectUtils.isEmpty(buildingEntity.getIntensity()) && PlatformObjectUtils.isEmpty(buildingEntity.getBuildYears()) && PlatformObjectUtils.isEmpty(buildingEntity.getPurpose()) && PlatformObjectUtils.isEmpty(buildingEntity.getLayerNum()) && PlatformObjectUtils.isEmpty(buildingEntity.getHeight()) && PlatformObjectUtils.isEmpty(buildingEntity.getTerrainConditions()) && PlatformObjectUtils.isEmpty(buildingEntity.getFoundationCondition())
                && PlatformObjectUtils.isEmpty(buildingEntity.getPlanarRule()) && PlatformObjectUtils.isEmpty(buildingEntity.getVerticalRule()) && PlatformObjectUtils.isEmpty(buildingEntity.getRoofComponent()) && PlatformObjectUtils.isEmpty(buildingEntity.getHousingCondition()) &&PlatformObjectUtils.isEmpty( buildingEntity.getDegreeDescription()) && PlatformObjectUtils.isEmpty(buildingEntity.getSeismicCapacity()) && PlatformObjectUtils.isEmpty(buildingEntity.getRemarks())){
            result.setMsg(message.toString());
            return result;
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getTaskCode())) {
            result.setSuccess(false);
            message.append("任务编号 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getProvince())) {
            result.setSuccess(false);
            message.append("建筑地点-省 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getCity())) {
            result.setSuccess(false);
            message.append("建筑地点-市 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getCounty())) {
            result.setSuccess(false);
            message.append("建筑地点-县 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getCountry())) {
            result.setSuccess(false);
            message.append("建筑地点-乡 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getLongitude())) {
            result.setSuccess(false);
            message.append("经度 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getLatitude())) {
            result.setSuccess(false);
            message.append("纬度 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getIntensity())) {
            result.setSuccess(false);
            message.append("设防烈度 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getFieldSurveyName())) {
            result.setSuccess(false);
            message.append("所属调查点 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getBuildYears())) {
            result.setSuccess(false);
            message.append("建造年代 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getPurpose())) {
            result.setSuccess(false);
            message.append("用途 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getHeight())) {
            result.setSuccess(false);
            message.append("高度 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getLayerNum())) {
            result.setSuccess(false);
            message.append("层数 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getTerrainConditions())) {
            result.setSuccess(false);
            message.append("地形条件（有无山体滑坡、山崩危险） 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getFoundationCondition())) {
            result.setSuccess(false);
            message.append("地基情况（有无下部软弱土层） 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getPlanarRule())) {
            result.setSuccess(false);
            message.append("平面型规则 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getVerticalRule())) {
            result.setSuccess(false);
            message.append("竖向规则性 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getRoofComponent())) {
            result.setSuccess(false);
            message.append("出屋面构件 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getHousingCondition())) {
            result.setSuccess(false);
            message.append("房屋状况 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getDegreeDescription())) {
            result.setSuccess(false);
            message.append("程度描述 不能为空、");
        }
        if (PlatformObjectUtils.isNotEmpty(buildingEntity.getDegreeDescription())) {

            if (buildingEntity.getDegreeDescription().length()>500){
                result.setSuccess(false);
                message.append("程度描述大于字数限制请缩短描述字数至250个汉字内、");
            }
        }
        if (PlatformObjectUtils.isEmpty(buildingEntity.getSeismicCapacity())) {
            result.setSuccess(false);
            message.append("抗震能力 不能为空、");
        }
        //校验任务编号是否存在
        List<String> allTaskNum = new ArrayList<>();
        List<Map<String, Object>> taskNum = fieldSurveyTaskService.getAllTaskNum();
        if (!PlatformObjectUtils.isEmpty(taskNum)) {
            for (Map<String, Object> map : taskNum) {
                String taskCode = map.get("task_num").toString();
                allTaskNum.add(taskCode);
            }
            if(!allTaskNum.contains(buildingEntity.getTaskCode())){
                result.setSuccess(false);
                message.append("任务编号不存在,请重新填写。");
            }

            //所属调查点。需要检查输入的是否正确。需要是这个任务下的调查点名称才行
            if(!PlatformObjectUtils.isEmpty(buildingEntity.getFieldSurveyName())){
                List<String> allFieldSurveyName = new ArrayList<>();
                List<Map<String, Object>> fieldSurveyNames = samplingService.getFieldSurveyNameByTaskCode(buildingEntity.getTaskCode());
                if (!PlatformObjectUtils.isEmpty(fieldSurveyNames)) {
                    for (Map<String, Object> map : fieldSurveyNames) {
                        String fieldSurveyName = map.get("field_survey_name").toString();
                        allFieldSurveyName.add(fieldSurveyName);
                    }
                    if(!allFieldSurveyName.contains(buildingEntity.getFieldSurveyName().trim())){
                        result.setSuccess(false);
                        message.append("所属调查点不在 "+ buildingEntity.getTaskCode() +"任务编号中,请重新填写。");
                    }
                }
            }
        }else{
            result.setSuccess(false);
            message.append("任务编号不存在,请重新填写。");
        }
        result.setMsg(message.toString());
        return result;
    }
}
